package com.cheer.ba.view.activity;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.cheer.ba.R;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.view.base.BaseAty;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：Administrator on 2016/6/22 0022 11:07
 * 功能：引导页
 * 说明：展示引导页
 */
public class GuideAty extends BaseAty implements ViewPager.OnPageChangeListener {

    private ViewPager mViewPager;
    /**
     * 滑动的点
     */
    private LinearLayout mLayoutPoint;
    /**
     * 存储滑动页面
     */
    private List<View> mViewList;
    /**
     * 存储滑动的点
     */
    private ImageView[] mDotImg;

    @Override
    public View loadView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.aty_guide, null);
    }

    @Override
    public void initPresenter() {
    }

    @Override
    protected boolean isTitleBarShow() {
        return false;
    }

    @Override
    protected boolean isSupportSwipeback() {
        return false;
    }

    @Override
    public void initView() {
        mViewPager = (ViewPager) findViewById(R.id.id_guide_viewp);
        mLayoutPoint = (LinearLayout) findViewById(R.id.id_guide_point);
    }

    @Override
    public void initData() {
        LayoutInflater mInflater = LayoutInflater.from(aty);
        mViewList = new ArrayList<View>();
        mViewList.clear();
        mViewList.add(mInflater.inflate(R.layout.guide_vp_one, null));
        mViewList.add(mInflater.inflate(R.layout.guide_vp_two, null));
//        mViewList.add(mInflater.inflate(R.layout.guide_vp_three, null));
//        mViewList.add(mInflater.inflate(R.layout.guide_vp_four, null));
//        mViewList.add(mInflater.inflate(R.layout.guide_vp_five, null));
        mViewList.add(mInflater.inflate(R.layout.guide_vp_six, null));

        mDotImg = new ImageView[mViewList.size()];
        for (int i = 0; i < mDotImg.length; i++) {
            mDotImg[i] = new ImageView(this);
            if (0 == i) {
                mDotImg[i].setBackgroundResource(R.drawable.dot_on);
            } else {
                mDotImg[i].setBackgroundResource(R.drawable.dot_off);
            }
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(15, 0, 15, 0);
            mDotImg[i].setLayoutParams(params);
            mLayoutPoint.addView(mDotImg[i]);
        }
        mViewPager.setAdapter(new ViewPagerAdapter());
        mViewPager.setOnPageChangeListener(this);
        mViewPager.setCurrentItem(0);
    }

    class ViewPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return mViewList.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ((ViewPager) container).addView(mViewList.get(position));
            return mViewList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView(mViewList.get(position));
        }
    }

    /*
     * state：网上通常说法：1的时候表示正在滑动，2的时候表示滑动完毕了，0的时候表示什么都没做，就是停在那；
     * 我的认为：1是按下时，0是松开，2则是新的标签页的是否滑动了
     * (例如：当前页是第一页，如果你向右滑不会打印出2，如果向左滑直到看到了第二页，那么就会打印出2了)； 个人认为一般情况下是不会重写这个方法的
     */
    @Override
    public void onPageScrollStateChanged(int arg0) {
    }

    /*
     * page：看名称就看得出，当前页； positionOffset：位置偏移量，范围[0,1]；
     * positionoffsetPixels：位置像素，范围[0,屏幕宽度)； 个人认为一般情况下是不会重写这个方法的
     */
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    @Override
    public void onPageSelected(int page) {
        // 更新图标
        for (int i = 0; i < mViewList.size(); i++) {
            if (page == i) {
                mDotImg[i].setBackgroundResource(R.drawable.dot_on);
            } else {
                mDotImg[i].setBackgroundResource(R.drawable.dot_off);
            }
        }
        if (page == mViewList.size() - 1) {
            Button mWelBtn = (Button) mViewList.get(page).findViewById(
                    R.id.id_guide_btn);
            mWelBtn.setVisibility(View.VISIBLE);
            mLayoutPoint.setVisibility(View.GONE);
            Animation mGuide_Alpha = new AlphaAnimation(0.0f, 1.0f);
            mGuide_Alpha.setDuration(5000);
            mGuide_Alpha.setInterpolator(new LinearInterpolator());
            mWelBtn.startAnimation(mGuide_Alpha);
            mWelBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Session.setUserIsFirstDownload(false);
                    skipActivity(aty, SplashAty.class);
                }
            });
        } else {
            mLayoutPoint.setVisibility(View.VISIBLE);
        }
    }
}
